package com.ls.fw.office;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

/**
 * 
 * @author lisheng
 * 2016年9月6日 下午10:20:53
 * @version V1.0
 */
public class CsvReader {
	private CSVParser csvReader = null;

	public CsvReader(String path, CSVFormat format) throws IOException {
		super();
		Reader reader = (new FileReader(path));
		csvReader = new CSVParser(new BufferedReader(reader), format);
	}

	public CsvReader(Reader reader, CSVFormat format) throws IOException {
		super();
		csvReader = new CSVParser(new BufferedReader(reader), format);
	}

	public CsvReader(InputStream is,String charset, CSVFormat format) throws IOException {
		super();
		Reader reader = (new InputStreamReader(is,Charset.forName(charset)));
		csvReader = new CSVParser(new BufferedReader(reader), format);
	}

	public CsvReader(URL url,String charset, CSVFormat format) throws IOException {
		super();
		csvReader = CSVParser.parse(url, Charset.forName(charset), format);
	}

	public CsvReader(InputStream is, CSVFormat format) throws IOException {
		super();
		Reader reader = (new InputStreamReader(is,Charset.forName("UTF-8")));
		csvReader = new CSVParser(new BufferedReader(reader), format);
	}

	public CsvReader(URL url, CSVFormat format) throws IOException {
		super();
		csvReader = CSVParser.parse(url, Charset.forName("UTF-8"), format);
	}

	public CsvReader(String path) throws IOException {
		super();
		Reader reader = (new FileReader(path));
		csvReader = new CSVParser(new BufferedReader(reader), CSVFormat.DEFAULT.withHeader());
	}

	public CsvReader(Reader reader) throws IOException {
		super();
		csvReader = new CSVParser(new BufferedReader(reader), CSVFormat.DEFAULT.withHeader());
	}

	public CsvReader(InputStream is,String charset) throws IOException {
		super();
		Reader reader = (new InputStreamReader(is,Charset.forName(charset)));
		csvReader = new CSVParser(new BufferedReader(reader), CSVFormat.DEFAULT.withHeader());
	}

	public CsvReader(URL url,String charset) throws IOException {
		super();
		csvReader = CSVParser.parse(url, Charset.forName(charset), CSVFormat.DEFAULT.withHeader());
	}
	public CsvReader(InputStream is) throws IOException {
		super();
		Reader reader = (new InputStreamReader(is,Charset.forName("UTF-8")));
		csvReader = new CSVParser(new BufferedReader(reader), CSVFormat.DEFAULT.withHeader());
	}

	public CsvReader(URL url) throws IOException {
		super();
		csvReader = CSVParser.parse(url, Charset.forName("UTF-8"), CSVFormat.DEFAULT.withHeader());
	}

	public Map<String, Integer> getHeadMap() {
		return csvReader.getHeaderMap();
	}

	public List<CSVRecord> getRecords() throws IOException {
		return csvReader.getRecords();
	}

	public CsvReader scan(CSVRecordMappr mapper) throws IOException {
		Iterator<CSVRecord> iterator = csvReader.iterator();
		while (iterator.hasNext()) {
			mapper.mapper(csvReader.getRecordNumber(), iterator.next());
		}
    	return this;
	}
	

	public static interface CSVRecordMappr {
		void mapper(long number, CSVRecord record);
	}

	public Iterator<CSVRecord> iterator() {
		return csvReader.iterator();
	}

	public CSVParser getCsvReader() {
		return csvReader;
	}

	public void close() throws IOException {
		if(!this.csvReader.isClosed()){
			this.csvReader.close();
		}
	}

	public static void main(String[] args) throws IOException {
		 
	}
}